package com.logictech.activity.system.interceptors;

import org.springframework.stereotype.Component;

import com.logictech.common.util.SystemEnum;
import com.logictech.dto.base.BaseResp;

@Component
public class ResponseHolder {
	private static ThreadLocal<Object> model = new ThreadLocal<>();

	public void clean() {
		model.remove();
	}

	public BaseResp getModel() {
		Object o = model.get();
		if (o == null) {
			this.setModel(new BaseResp());
			o = this.getObject();
		}
		if (o != null && o instanceof BaseResp) {
			return (BaseResp) o;
		} else {
			return null;
		}
	}

	public Object getObject() {
		return model.get();
	}

	public void setModel(Object o) {
		model.set(o);
	}

	public BaseResp setErrorMsg(SystemEnum systemEnum) {
		BaseResp responseModel = this.getModel();
		responseModel.setCode(systemEnum.getErrCode());
		responseModel.setMsg(systemEnum.getErrChineseMsg());
		return responseModel;
	}
}